home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / dscap.exe / DSCAP.C next >
C/C++ Source or Header  |  1993-06-08  |  13KB  |  504 lines

  1. /****************************************************************************/
  2. /*                             D S C A P . C                                */
  3. /*                                                                          */
  4. /*  Written By:  Clint McVey                                                */
  5. /*        Date:  05/01/93                                                   */
  6. /* Environment:  BC 3.1                                                     */
  7. /* Description:  DS Capture Example                                         */
  8. /*                                                                          */
  9. /*     DSCAP                is provided as is and carries no warranty       */
  10. /*     whatsoever.  Novell disclaims and excludes any and all implied       */
  11. /*     warranties of merchantability, title and fitness for a particular    */
  12. /*     purpose.  Novell does not warrant that the software will satisfy     */
  13. /*     your requirements or that the software is without defect or error    */
  14. /*     or that operation of the software will be uninterrupted.  You are    */
  15. /*     using the software at your risk.  The software is not a product      */
  16. /*     of Novell, Inc. or any of subsidiaries.                              */
  17. /*                                                                          */
  18. /* NOTE:                                                                    */
  19. /*     Always reserve at least 8K of stack of DS functions.                 */
  20. /*                                                                          */
  21. /****************************************************************************/
  22.  
  23. //
  24. // At least 8K of stack should be reserved for DS functions.
  25. //
  26. unsigned _stklen = 8192;
  27.  
  28. #define NWDOS
  29.  
  30. #include <stdlib.h>
  31. #include <dos.h>
  32. #include <string.h>
  33. #include <process.h>
  34. #include <io.h>
  35. #include <stdio.h>
  36. #include <conio.h>
  37. #include <memory.h>
  38. #include <nwcalls.h>
  39. #include <nwlocale.h>
  40. #include <nwnet.h>
  41. #include <nwerror.h>
  42.  
  43. NWCAPTURE_FLAGS1
  44.                 captureFlags1;
  45. NWCAPTURE_FLAGS2
  46.                 captureFlags2;
  47. NWCONN_HANDLE
  48.                 defaultConnHandle;
  49. NWLPT         LPTDevice;
  50. Buf_T    NWFAR *Names = NULL;
  51. Buf_T    NWFAR *objectInfo = NULL;
  52. Buf_T    NWFAR *myFilter = NULL;
  53. NWDSContextHandle
  54.                 context;
  55.  
  56. void PrintOut( void );
  57. void ResetToDefaults( void );
  58. void CleanUp( int );
  59.  
  60. void main(int argc,char *argv[])
  61.     {
  62.     LCONV     convert;
  63.     int      NDSPresent;
  64.     int        err,i,j;
  65.     Buf_T    NWFAR
  66.                 *Names = NULL;
  67.     Buf_T    NWFAR
  68.                 *objectInfo = NULL;
  69.     Buf_T    NWFAR
  70.                 *myFilter = NULL;
  71.     Filter_Cursor_T NWFAR
  72.                 *filterCursor = NULL;
  73.     char         serverName[MAX_DN_CHARS+2];
  74.     char        attrName[] = {"Host Server"}; // Largest attr MAX_SCHEMA_NAME+2
  75.     NWDS_ITERATION
  76.                 iterationHandle;
  77.     NWDS_NUM_OBJ
  78.                 objectsSearched=0;
  79.  
  80.     char                objectName[MAX_DN_CHARS+2];
  81.     NWCCODE             cCode;
  82.     NWDSCCODE        cDSCode;
  83.     NWCONN_HANDLE     connHandle;
  84.     NWOBJ_ID            queueID;
  85.     NWLOCAL_MODE    reserved1;
  86.     NWLOCAL_SCOPE    reserved2;
  87.     NWCOUNT            objectCount;
  88.     NWOBJECT_INFO    *info;
  89.     NWCOUNT            attrCount;
  90.     char                cannonName[MAX_DN_CHARS+2]; 
  91.     NWCOUNT            attrValCount;
  92.     void NWFAR        *attrVal;
  93.     NWSYNTAX_ID        syntaxID;
  94.     NWSIZE            attrValSize;
  95.  
  96.     if (argc > 1)
  97.         {
  98.         strcpy(objectName,argv[1]);
  99.         }
  100.     else
  101.         {
  102.         printf("\Invalid Number Of Arguments!  Usage:  DSCAP QUEUE");
  103.         putch(7);
  104.         exit(1);
  105.         }
  106.     cCode=NWCallsInit(NULL,NULL);
  107.     if(cCode != 0)
  108.         {
  109.         printf("\nFailure in NWCallsInit.  Status: %04X  Line: %d",cCode, __LINE__);
  110.         exit(1);
  111.         }
  112.     NWLsetlocale(LC_ALL, "");
  113.     NWLlocaleconv((LCONV NWFAR *)&convert);
  114.  
  115.     NDSPresent = NWIsDSAuthenticated();
  116.     if (NDSPresent)
  117.         {
  118.         cCode = NWInitUnicodeTables(convert.country_id,
  119.                                              convert.code_page);
  120.         if (cCode)
  121.             {
  122.             printf("\nNWInitUnicodeTables: failed %04x", cCode);
  123.             exit(1);
  124.             }
  125.         }
  126.     else
  127.         {
  128.         printf("\nNot a DS connection...program terminating...");
  129.         exit(0);
  130.         }
  131. //
  132. // Create context and check for errors.
  133. //
  134.     if((context = NWDSCreateContext()) == ERR_CONTEXT_CREATION)
  135.         {
  136.         printf("Could not create context. Must load on a 4.0 server.\n");
  137.         exit(0);
  138.         }
  139.  
  140.     cDSCode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &Names);
  141.     if(cDSCode)
  142.         {
  143.         printf("Error in NWDSAllocBuf: %d  Line: %d\n",cDSCode, __LINE__);
  144.         CleanUp(cDSCode);
  145.         }
  146.     cDSCode=NWDSInitBuf(context, DSV_SEARCH, Names);
  147.     if(cDSCode)
  148.         {
  149.         printf("Error in NWDSInitBuf: %d  Line: %d\n", cDSCode, __LINE__);
  150.         CleanUp(cDSCode);
  151.         }
  152.     cDSCode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &myFilter);
  153.     if(cDSCode)
  154.         {
  155.         printf("Error in NWDSAllocBuf: %d  Line: %d\n",cDSCode, __LINE__);
  156.         CleanUp(cDSCode);
  157.         }
  158.     cDSCode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &objectInfo);
  159.     if(cDSCode)
  160.         {
  161.         printf("Error in NWDSAllocBuf: %d  Line:  %d\n",cDSCode, __LINE__);
  162.         CleanUp(cDSCode);
  163.         }
  164.     cDSCode = NWDSInitBuf(context,
  165.                                  DSV_SEARCH_FILTER,
  166.                                  objectInfo);
  167.     if(cDSCode)
  168.         {
  169.         printf("Error in NWDSInitBuf: %d  Line:  %d\n",cDSCode, __LINE__);
  170.         CleanUp(cDSCode);
  171.         }
  172.     cDSCode = NWDSInitBuf(context,
  173.                                  DSV_SEARCH_FILTER,
  174.                                  myFilter);
  175.     if(cDSCode)
  176.         {
  177.         printf("Error in NWDSInitBuf: %d  Line: %d\n",cDSCode, __LINE__);
  178.         CleanUp(cDSCode);
  179.         }
  180.     cDSCode = NWDSAllocFilter(&filterCursor);
  181.     if(cDSCode)
  182.         {
  183.         NWDSFreeFilter(filterCursor,NULL);
  184.         printf("Error in NWDSAllocFilter: %d  Line: %d\n",cDSCode, __LINE__);
  185.         CleanUp(cDSCode);
  186.         }
  187.     cDSCode = NWDSAddFilterToken(filterCursor,
  188.                                           FTOK_ANAME,
  189.                                           A_OBJECT_CLASS,
  190.                                           SYN_CLASS_NAME);
  191.     if(cDSCode)
  192.         {
  193.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  194.         NWDSFreeFilter(filterCursor,NULL);
  195.         CleanUp(cDSCode);
  196.         }
  197.     cDSCode = NWDSAddFilterToken(filterCursor,
  198.                                           FTOK_EQ,
  199.                                           NULL,
  200.                                           NULL);
  201.     if(cDSCode)
  202.         {
  203.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  204.         NWDSFreeFilter(filterCursor,NULL);
  205.         CleanUp(cDSCode);
  206.         }
  207.     cDSCode = NWDSAddFilterToken(filterCursor,
  208.                                           FTOK_AVAL,
  209.                                           A_QUEUE,
  210.                                           SYN_CLASS_NAME);
  211.     if(cDSCode)
  212.         {
  213.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  214.         NWDSFreeFilter(filterCursor,NULL);
  215.         CleanUp(cDSCode);
  216.         }
  217.     cDSCode = NWDSAddFilterToken(filterCursor,
  218.                                           FTOK_AND,
  219.                                           NULL,
  220.                                           NULL);
  221.     if(cDSCode)
  222.         {
  223.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  224.         NWDSFreeFilter(filterCursor,NULL);
  225.         CleanUp(cDSCode);
  226.         }
  227.     cDSCode = NWDSAddFilterToken(filterCursor,
  228.                                           FTOK_ANAME,
  229.                                           A_COMMON_NAME,
  230.                                           SYN_CI_STRING);
  231.     if(cDSCode)
  232.         {
  233.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  234.         NWDSFreeFilter(filterCursor,NULL);
  235.         CleanUp(cDSCode);
  236.         }
  237.     cDSCode = NWDSAddFilterToken(filterCursor,
  238.                                           FTOK_EQ,
  239.                                           NULL,
  240.                                           NULL);
  241.     if(cDSCode)
  242.         {
  243.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  244.         NWDSFreeFilter(filterCursor,NULL);
  245.         CleanUp(cDSCode);
  246.         }
  247.     cDSCode = NWDSAddFilterToken(filterCursor,
  248.                                           FTOK_AVAL,
  249.                                           (void *)objectName,
  250.                                           SYN_CI_STRING);
  251.     if(cDSCode)
  252.         {
  253.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  254.         NWDSFreeFilter(filterCursor,NULL);
  255.         CleanUp(cDSCode);
  256.         }
  257.     cDSCode = NWDSAddFilterToken(filterCursor,
  258.                                           FTOK_END,
  259.                                           NULL,
  260.                                           NULL);
  261.     if(cDSCode)
  262.         {
  263.         printf("Error in NWDSAddFilterToken: %d  Line: %d\n",cDSCode, __LINE__);
  264.         NWDSFreeFilter(filterCursor,NULL);
  265.         CleanUp(cDSCode);
  266.         }
  267.     cDSCode = NWDSPutFilter(context,
  268.                                     myFilter,
  269.                                     filterCursor,
  270.                                     NULL);
  271.     if(cDSCode)
  272.         {
  273.         printf("Error in NWDSPutFilter: %d  Line: %d\n",cDSCode, __LINE__);
  274.         CleanUp(cDSCode);
  275.         }
  276.     cDSCode = NWDSPutAttrName(context, Names, attrName);
  277.     if(cDSCode)
  278.         {
  279.         printf("Error in NWDSPutAttrName: %d  Line:  %d\n",cDSCode, __LINE__);
  280.         CleanUp(cDSCode);
  281.         }
  282.     iterationHandle=0xFFFFFFFF;
  283.     cDSCode = NWDSSearch(context,
  284.                                 DS_ROOT_NAME,
  285.                                 DS_SEARCH_SUBTREE,
  286.                                 FALSE,
  287.                                 myFilter,
  288.                                 DS_ATTRIBUTE_VALUES,
  289.                                 FALSE,
  290.                                 Names,
  291.                                 &iterationHandle,
  292.                                 1,
  293.                                 &objectsSearched,
  294.                                 objectInfo);
  295.     if(cDSCode)
  296.         {
  297.         printf("Error in NWDSSearch: %04X  Line: %d\n",cDSCode, __LINE__);
  298.         CleanUp(cDSCode);
  299.         }
  300.     if(iterationHandle != -1L)
  301.         {
  302.         cDSCode=NWDSCloseIteration(context,
  303.                                             iterationHandle,
  304.                                             DSV_CLOSE_ITERATION);
  305.         if(cDSCode)
  306.             {
  307.             printf("Error in NWDSCloseIteration: %04X  Line: %d\n",cDSCode, __LINE__);
  308.             CleanUp(cDSCode);
  309.             }
  310.         }
  311.     cDSCode = NWDSGetObjectCount(context,
  312.                                           objectInfo,
  313.                                           &objectCount);
  314.     if(cDSCode)
  315.         {
  316.         printf ("Error in NWDSGetObjectCount: %d  Line: %d\n", cCode, __LINE__);
  317.         CleanUp(cDSCode);
  318.         }
  319.     if(objectCount == 0)
  320.         {
  321.         printf("%s was not found in your default context.\n",objectName);
  322.         printf("\tPlease specify a valid queue name.\n");
  323.         putch(7);
  324.         cDSCode=0xFFFF;
  325.         CleanUp(cDSCode);
  326.         }
  327.     info = malloc(sizeof(Object_Info_T));
  328.  
  329.     for(;objectCount>0; --objectCount)
  330.         {
  331.         cDSCode = NWDSGetObjectName(context,
  332.                                              objectInfo,
  333.                                              objectName,
  334.                                              &attrCount,
  335.                                              info);
  336.  
  337.         if(cDSCode)
  338.             {
  339.             printf("Error in NWDSGetObjectName: %d  Line:  %d\n", cCode, __LINE__);
  340.             objectCount = -1;
  341.             attrCount = -1;
  342.             }
  343.  
  344.         for (;attrCount>0; --attrCount)
  345.             {
  346.             cCode = NWDSGetAttrName(context,
  347.                                             objectInfo,
  348.                                             attrName,
  349.                                             &attrValCount,
  350.                                             &syntaxID);
  351.             if (cDSCode)
  352.                 {
  353.                 printf ("Error in NWDSGetAttrName: %d  Line: %d\n",cCode,__LINE__);
  354.                 break;
  355.                 }
  356.             if (attrValCount > 1)
  357.                 {
  358.                 printf ("Invalid DS Connection.  Line: %d\n",__LINE__);
  359.                 break;
  360.                 }
  361.             for (;attrValCount>0; --attrValCount)
  362.                 {
  363.                 cDSCode = NWDSComputeAttrValSize(context,
  364.                                                          objectInfo,
  365.                                                          syntaxID,
  366.                                                          &attrValSize);
  367.  
  368.                 if(cDSCode)
  369.                     {
  370.                     printf ("Error in NWDSComputeAttrValSize: %d  Line: %d\n",cCode,__LINE__);
  371.                     break;
  372.                     }
  373.  
  374.                 if((attrVal = malloc(attrValSize)) == NULL)
  375.                     {
  376.                     printf ("Unable to allocate memory for attribute value.  Line: %d\n",cCode,__LINE__);
  377.                     CleanUp(cDSCode);
  378.                     }
  379.  
  380.                 cCode = NWDSGetAttrVal(context,
  381.                                               objectInfo,
  382.                                               syntaxID,
  383.                                               attrVal);
  384.                 if(cDSCode)
  385.                     {
  386.                     printf ("Error in NWDSGetAttrVal: %d  Line: %d\n",cCode,__LINE__);
  387.                     free(attrVal);
  388.                     break;
  389.                     }
  390.                 strcpy(serverName,attrVal);
  391.                 free(attrVal);
  392.                 }
  393.             }
  394.         }
  395. //
  396. // CleanUp DS Resources, but don't terminate
  397. //
  398.     CleanUp(0);
  399. //
  400. // Extract Bindery "Style" Name
  401. //
  402.     for(i=0,j=0;i<strlen(serverName);i++)
  403.         {
  404.         if(serverName[i] == '.')
  405.             {
  406.             if(i == 0)
  407.                 {
  408.                 if(serverName[i+3] == '=')
  409.                     i+=4;
  410.                 else
  411.                     i++;
  412.                 continue;
  413.                 }
  414.             else
  415.                 {
  416.                 serverName[j] = NULL;
  417.                 break;
  418.                 }
  419.             }
  420.             serverName[j] = serverName[i];
  421.             if(serverName[i] == NULL) break;
  422.             j++;
  423.         }
  424.     cCode=NWAttachToFileServer(serverName,0,&connHandle);
  425.     if((cCode != 0) && (cCode != 0x8800))
  426.         {
  427.         printf("Unable to attach to %s.  Status:  %04X  Line:  %d\n",serverName,cCode,__LINE__);
  428.         NWDSFreeContext(context);
  429.         exit(1);
  430.         }
  431.     cDSCode=NWDSMapNameToID(context,connHandle,objectName,&queueID);
  432.     if(cDSCode)
  433.         {
  434.         printf("Error in NWDSMapNameToID. Status:  %d  Line:  %d\n",cDSCode,__LINE__);
  435.         NWDSFreeContext(context);
  436.         putch(7);
  437.         exit(1);
  438.         }
  439.     NWDSFreeContext(context);
  440.  
  441.     LPTDevice=0x01;
  442.     cCode=NWCancelCapture(LPTDevice);
  443.     if((cCode != 0) && (cCode != 0x884C))
  444.         {
  445.         printf("\nUnable to cancel capture.  Status:  %04X  Line: %d",cCode,__LINE__);
  446.         exit(1);
  447.         }
  448.     cCode=NWStartQueueCapture(connHandle,LPTDevice,queueID,objectName);
  449.     if(cCode != 0)
  450.         {
  451.         printf("\nError starting queue capture.  Status = %04X",cCode);
  452.         exit(1);
  453.         }
  454.  
  455.     cCode=NWGetCaptureFlags(LPTDevice,&captureFlags1,&captureFlags2);
  456.  
  457.     captureFlags1.numCopies=0x05;
  458.     captureFlags1.flushCaptureTimeout=(36);
  459.     captureFlags1.printFlags += 0x10; /* Notify When Done */
  460.     strcpy(captureFlags1.bannerText,"DS Capture!");
  461.     cCode=NWSetCaptureFlags(connHandle,LPTDevice,&captureFlags1);
  462.     if(cCode != 0)
  463.         {
  464.         printf("Error Setting Capture Flags.  Status: %04X  Line:  %d\n",cCode,__LINE__);
  465.         exit(1);
  466.         }
  467.     PrintOut();
  468.     cCode=NWEndCapture(LPTDevice);
  469.     if(cCode != 0)
  470.         {
  471.         printf("Error in NWEndCapture.  Status: %04X  Line:  %d\n",cCode,__LINE__);
  472.         exit(1);
  473.         }
  474.     printf("Successfully sent print data to %s.\n",objectName);
  475.     exit(0);
  476.     }
  477.  
  478. void PrintOut( void )
  479.     {
  480.     fprintf(stdprn,"This is a printer test");
  481.     fflush(stdprn);
  482.     }
  483.  
  484. void CleanUp ( int cDSCode )
  485.     {
  486.     if( Names )
  487.         NWDSFreeBuf(Names);
  488.     if( objectInfo )
  489.         NWDSFreeBuf(objectInfo);
  490.     if( myFilter )
  491.         NWDSFreeBuf(myFilter);
  492.     if(cDSCode)
  493.         {
  494.         NWDSFreeContext(context);
  495.         exit(1);
  496.         }
  497.     }
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.